package cn.rengy.web.excelcompare.service;

import cn.rengy.isql.Dao;
import cn.rengy.lang.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 比较定义
 */
@Service
public class ExcelCompareConfigService {

    public static void main(String[] args) {
        System.out.println(Long.MAX_VALUE);
    }
    @Autowired
    private Dao dao;

    @Autowired
    private ExcelCompareTaskService excelCompareTaskService;

    @Autowired
    private ExcelUpdateConfigService excelUpdateConfigService;
    /**
     * 删除比较定义
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResultEntity deleteById(Long id){
        //1.删除比较定义明细
        dao.update("delete from excel_compare_config_detail where compare_config_id=?",id);
        //2.删除字段映射 excel_cell_mapping
        dao.update("delete from excel_cell_mapping where compare_config_id=?",id);
        //3.删除比较任务
        List<Long> taskIdList=dao.querySingleColumnForList("select id from excel_compare_task where compare_config_id=?",Long.class,id);
        for(Long taskId:taskIdList){
            excelCompareTaskService.deleteById(taskId);
        }
        //4.删除更新配置
        List<Long> updateIdList=dao.querySingleColumnForList("select id from excel_update_config where compare_config_id=?",Long.class,id);
        for(Long updateId:updateIdList){
            excelUpdateConfigService.deleteById(updateId);
        }
        //最后.删除比较定义
        dao.update("delete from excel_compare_config where id=?",id);
        return ResultEntity.ok();
    }
}
